﻿using Chain.BLL;
using Chain.DBUtility;
using Chain.Model;
using System;
using System.Text;


public class MEMMemRateInfoUpdate
{
    private static Chain.BLL.Mem bll = new Chain.BLL.Mem();
    private static string MEMCard;
    private static string MEMName;
    private static bool RateType;
    private static decimal[] MEMRate = new decimal[15];
    private static int NowRateLevel;
    private static int RateLevel;
    private static string PointOrderCode;
    public static void MEMMemRateInfo(Chain.Model.Mem mem, int point, string OrderAccount, int pointType, int UserID)
    {
        Chain.Model.SysUser User = new Chain.BLL.SysUser().GetModel(UserID);
        Chain.Model.PointRate pointRate = new Chain.BLL.PointRate().GetPointRate();
        Chain.Model.SysParameter modelSysParameter = new Chain.BLL.SysParameter().GetModel(1);
        RateType = pointRate == null ? false : pointRate.PointRateType;
        RateLevel = 1;
        NowRateLevel = 1;
        PointOrderCode = OrderAccount;
        MEMCard = mem.MemCard;
        MEMName = mem.MemName;
        if (User != null && modelSysParameter.PointLevel)
        {
            MEMRate[0] = GetMemRateInfo("MemID="+mem.MemID+" order by datetime desc");
            MemRateInfoUpdate(mem, point, pointType, mem.MemID, User);
        }
    }

    public static void MemRateInfoUpdate(Chain.Model.Mem mem, int point, int pointType, int mymemID, Chain.Model.SysUser User)
    {
        if ((NowRateLevel <= RateLevel) && (mem.MemRecommendID != 0))
        {
            Chain.Model.Mem TEMPMEM = bll.GetModel(mem.MemRecommendID);

            if (TEMPMEM != null)
            {
                int temp = 0;
                if (RateType)
                {
                    temp = Convert.ToInt32(decimal.Truncate(point * (MEMRate[NowRateLevel - 1] / 100M)));
                }
                else
                {
                    temp = Convert.ToInt32(MEMRate[NowRateLevel - 1]);
                }
                if (PubFunction.IsShopPoint(User.UserShopID, ref temp))
                {
                    TEMPMEM.MemPoint += temp;
                    int flag = bll.Update(TEMPMEM);
                    PubFunction.SetShopPoint(User.UserID, User.UserShopID, temp, "会员消费，积分提成获得积分，扣除店铺积分。", 2);
                    if (flag == 1)
                    {
                        PubFunction.UpdateMemLevel(TEMPMEM);
                        Chain.Model.PointLog modelPoint = new Chain.Model.PointLog();
                        modelPoint.PointMemID = TEMPMEM.MemID;
                        modelPoint.PointNumber = temp;
                        modelPoint.PointChangeType = 9;
                        modelPoint.PointShopID = TEMPMEM.MemShopID;
                        modelPoint.PointCreateTime = DateTime.Now;
                        modelPoint.PointUserID = TEMPMEM.MemUserID;
                        modelPoint.PointOrderCode = PointOrderCode;
                        modelPoint.PointGiveMemID = mymemID;
                        switch (pointType)
                        {
                            case -1:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员商品消费退货，获得积分：" + temp.ToString();
                                break;

                            case 1:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员商品消费，获得积分：" + temp.ToString();
                                break;

                            case 2:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员快速消费，获得积分：" + temp.ToString();
                                break;

                            case 3:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员充次，获得积分：" + temp.ToString();
                                break;

                            case 5:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员注册，获得积分：" + temp.ToString();
                                break;

                            case 6:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员手动增加积分，获得积分：" + temp.ToString();
                                break;

                            case 14:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员充时增加积分，获得积分：" + temp.ToString();
                                break;

                            case 15:
                                modelPoint.PointRemark = "卡号为：" + MEMCard + ",姓名为" + MEMName + "的会员充值增加积分，获得积分：" + temp.ToString();
                                break;
                        }
                        new Chain.BLL.PointLog().Add(modelPoint);
                        PubFunction.SaveSysLog(TEMPMEM.MemUserID, 3, "会员积分提成", "会员" + TEMPMEM.MemName + "积分提成成功", TEMPMEM.MemShopID, DateTime.Now, PubFunction.ipAdress);
                    }
                    NowRateLevel++;
                    MemRateInfoUpdate(TEMPMEM, point, pointType, mymemID, User);
                }
            }
        }
    }
    public static int GetMemRateInfo(string strWhere)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("select top 1 rate FROM MemRateInfo ");
        if (strWhere.Trim() != "")
        {
            builder.Append(" where " + strWhere);
        }
        object single = DbHelperSQL.GetSingle(builder.ToString());
        if (single == null)
        {
            return 0;
        }
        return Convert.ToInt32(single);
    }
}
